/*******************************************************************************
 * Copyright 2017 osswangxining@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
/**
 * Copyright © 2016-2017 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.iotp.infomgt.dao.model.sql;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.iotp.infomgt.dao.model.BaseSqlEntity;
import org.iotp.infomgt.dao.model.ModelConstants;
import org.iotp.infomgt.dao.model.SearchTextEntity;
import org.iotp.infomgt.dao.util.mapping.JsonStringType;
import org.iotp.infomgt.data.User;
import org.iotp.infomgt.data.common.UUIDConverter;
import org.iotp.infomgt.data.id.CustomerId;
import org.iotp.infomgt.data.id.TenantId;
import org.iotp.infomgt.data.id.UserId;
import org.iotp.infomgt.data.security.Authority;

import com.datastax.driver.core.utils.UUIDs;
import com.fasterxml.jackson.databind.JsonNode;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * Created by Valerii Sosliuk on 4/21/2017.
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
@Table(name = ModelConstants.USER_PG_HIBERNATE_COLUMN_FAMILY_NAME)
public class UserEntity extends BaseSqlEntity<User> implements SearchTextEntity<User> {
  @Transient
  private static final long serialVersionUID = -271106508790582977L;

  @Column(name = ModelConstants.USER_TENANT_ID_PROPERTY)
  private String tenantId;

  @Column(name = ModelConstants.USER_CUSTOMER_ID_PROPERTY)
  private String customerId;

  @Enumerated(EnumType.STRING)
  @Column(name = ModelConstants.USER_AUTHORITY_PROPERTY)
  private Authority authority;

  @Column(name = ModelConstants.USER_EMAIL_PROPERTY, unique = true)
  private String email;

  @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY)
  private String searchText;

  @Column(name = ModelConstants.USER_FIRST_NAME_PROPERTY)
  private String firstName;

  @Column(name = ModelConstants.USER_LAST_NAME_PROPERTY)
  private String lastName;

  @Type(type = "json")
  @Column(name = ModelConstants.USER_ADDITIONAL_INFO_PROPERTY)
  private JsonNode additionalInfo;

  public UserEntity() {
  }

  public UserEntity(User user) {
    if (user.getId() != null) {
      this.setId(user.getId().getId());
    }
    this.authority = user.getAuthority();
    if (user.getTenantId() != null) {
      this.tenantId = UUIDConverter.fromTimeUUID(user.getTenantId().getId());
    }
    if (user.getCustomerId() != null) {
      this.customerId = UUIDConverter.fromTimeUUID(user.getCustomerId().getId());
    }
    this.email = user.getEmail();
    this.firstName = user.getFirstName();
    this.lastName = user.getLastName();
    this.additionalInfo = user.getAdditionalInfo();
  }

  @Override
  public String getSearchTextSource() {
    return email;
  }

  @Override
  public void setSearchText(String searchText) {
    this.searchText = searchText;
  }

  @Override
  public User toData() {
    User user = new User(new UserId(getId()));
    user.setCreatedTime(UUIDs.unixTimestamp(getId()));
    user.setAuthority(authority);
    if (tenantId != null) {
      user.setTenantId(new TenantId(UUIDConverter.fromString(tenantId)));
    }
    if (customerId != null) {
      user.setCustomerId(new CustomerId(UUIDConverter.fromString(customerId)));
    }
    user.setEmail(email);
    user.setFirstName(firstName);
    user.setLastName(lastName);
    user.setAdditionalInfo(additionalInfo);
    return user;
  }

}
